package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Cart;
import com.ericom.service.GenericManager;

public class CartAction extends BaseAction {
	private GenericManager<Cart, Long> cartManager;
	private List carts;
	private Cart cart;
	private Long id;

	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setCartManager(GenericManager<Cart, Long> cartManager) {
		this.cartManager = cartManager;
	}

	public String list() {
		carts = cartManager.getAll();
		return SUCCESS;
	}

	public List getCarts() {
		return carts;
	}

	public String delete() {
		cartManager.remove(cart.getId());
		this.saveMessage(getText("cart.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			cart = cartManager.get(id);
		} else {
			cart = new Cart();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (cart.getId() == null);
		cart = cartManager.save(cart);

		String key = (isNew) ? "cart.added" : "cart.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
